﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VIFON.BLL;
using VIFON.DAL;
namespace VIFON
{
    public partial class frmCustomerSummary : Telerik.WinControls.UI.RadForm
    {
        BLRegion blRegion = new BLRegion();
        public frmCustomerSummary()
        {
            InitializeComponent();
        } 
        public void SetCustomerFormatDTP()
        {
            dtpFrom.Format = DateTimePickerFormat.Custom;
            dtpFrom.CustomFormat = "MM/yyyy";
            dtpFrom.ShowUpDown = true;
            dtpTo.Format = DateTimePickerFormat.Custom;
            dtpTo.CustomFormat = "MM/yyyy";
            dtpTo.ShowUpDown = true;
        }
        private void LoadRegions()
        {
            ddlRegion.DisplayMember = "Name";
            ddlRegion.ValueMember = "Id";
            ddlRegion.DataSource = BLRegion.GetRegions();
            ddRegion2.DataSource = BLRegion.GetRegions();
        }
        private void CustomerSummary_Load(object sender, EventArgs e)
        {
            SetCustomerFormatDTP();                      
            LoadRegions();
        }
        //Area Tab 1
        private void ShowCustomerSummaryByCondition()
        {
             try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var result = from Customers in db.Customers
                                 from Products in db.Products
                                 from BillDetails in db.BillDetails
                                 from Bills in db.Bills
                                 where
                                   Customers.Id == Bills.CustomerId 
                                   && Products.Id == BillDetails.ProductId
                                   &&Bills.Id == BillDetails.BillId 
                                   &&Customers.Region == int.Parse(ddlRegion.SelectedValue.ToString())
                                   && Bills.CreatedDate.Month >= dtpFrom.Value.Date.Month
                                   && Bills.CreatedDate.Month <= dtpTo.Value.Date.Month
                                   && Bills.CreatedDate.Year >= dtpFrom.Value.Date.Year
                                   && Bills.CreatedDate.Year >= dtpTo.Value.Date.Year
                                 group new { Customers, Products, BillDetails } by new
                                 {
                                     Customers.Id,
                                     Customers.CustomerName,
                                     Customers.Address
                                 } into g
                                 select new
                                 {
                                     CustomerId = g.Key.Id,
                                     CustomerName = g.Key.CustomerName,
                                     CustomerAddress = g.Key.Address,
                                     ThanhTien = (double?)g.Sum(p => ((double)p.Products.Prices - ((double)p.Products.Prices * p.Customers.Discount / 100)) * (double)p.BillDetails.Amounts),
                                     TongSoLuong = (int?)g.Sum(p => p.BillDetails.Amounts)
                                 };
                    if (result != null)
                    {

                        gvshow.Rows.Clear();
                        foreach (var l in result)
                        {
                            gvshow.Rows.Add(l.CustomerId, l.CustomerAddress, l.CustomerName, l.TongSoLuong, l.ThanhTien, ddlRegion.Text);
                        }
                    }          

                }  
            }
            catch{throw;}
        }
        private void ShowCustomerSummary()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var result = from Customers in db.Customers
                                 from Products in db.Products
                                 from BillDetails in db.BillDetails
                                 from Bills in db.Bills
                                 where
                                   Customers.Id == Bills.CustomerId
                                   && Products.Id == BillDetails.ProductId
                                   && Bills.Id == BillDetails.BillId
                                   && Bills.CreatedDate.Month >= dtpFrom.Value.Date.Month
                                   && Bills.CreatedDate.Month <= dtpTo.Value.Date.Month
                                   && Bills.CreatedDate.Year >= dtpFrom.Value.Date.Year
                                   && Bills.CreatedDate.Year >= dtpTo.Value.Date.Year
                                 group new { Customers, Products, BillDetails } by new
                                 {
                                     Customers.Id,
                                     Customers.CustomerName,
                                     Customers.Address
                                 } into g
                                 select new
                                 {
                                    CustomerId=g.Key.Id,
                                    CustomerName=g.Key.CustomerName,
                                    CustomerAddress=g.Key.Address,
                                    ThanhTien = (double?)g.Sum(p => ((double)p.Products.Prices - ((double)p.Products.Prices * p.Customers.Discount / 100)) * (double)p.BillDetails.Amounts),
                                    TongSoLuong = (int?)g.Sum(p => p.BillDetails.Amounts)
                                 };
                    if (result != null)
                    {

                        gvshow.Rows.Clear();
                        foreach (var l in result)
                        {
                            gvshow.Rows.Add(l.CustomerId, l.CustomerAddress, l.CustomerName, l.TongSoLuong, l.ThanhTien, ddlRegion.Text);
                        }
                    }
                    gvshow.Refresh();
                }
            }
            catch { throw; }
        }
        private void btnxem_Click(object sender, EventArgs e)
        {
            //ShowCustomerSummary();
            DateTime timefrom = DateTime.Parse(dtpFrom.Text);
            DateTime timeto = DateTime.Parse(dtpTo.Text);
            if(timefrom>timeto)
            {
                MessageBox.Show("Ngày chọn sai. Vui lòng xem lại");
            }
            else
            {
                ShowCustomerSummaryByCondition();
            }            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ddlRegion.Text = "";
            DateTime timefrom = DateTime.Parse(dtpFrom.Text);
            DateTime timeto = DateTime.Parse(dtpTo.Text);
            if (timefrom > timeto)
            {
                MessageBox.Show("Ngày chọn sai. Vui lòng xem lại");
            }
            else
            {
                ShowCustomerSummary();
            }         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<ReportCustomerByRegion> reportlist = new List<ReportCustomerByRegion>();
            for (int i = 0; i < gvshow.Rows.Count; i++)
            {
                ReportCustomerByRegion report = new ReportCustomerByRegion();
                report.CustomerName = gvshow.Rows[i].Cells["colCustomerName"].Value.ToString();
                report.DateTime = dtpFrom.Text;
                report.Address = gvshow.Rows[i].Cells["colAddress"].Value.ToString();
                report.TotalMoney = int.Parse(gvshow.Rows[i].Cells["colTotal"].Value.ToString());
                report.TotalWeight = int.Parse(gvshow.Rows[i].Cells["colTotalWeight"].Value.ToString());
                report.RegionName = ddlRegion.Text;
                reportlist.Add(report);
            }
            frmReportByRegion frm = new frmReportByRegion(reportlist);
            frm.Show();
        }
        //Area Tab2
    }
}
